package com.hui.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.entity.dao.UserAddress;
import com.hui.entity.enums.ShopCommonEnum;
import com.hui.entity.vo.UserAddressVo;
import com.hui.mapper.UserAddressMapper;
import com.hui.model.addr.AddressParam;
import com.hui.service.UserAddressService;
import com.hui.util.BeanCopyUtils;
import com.hui.util.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户地址表(UserAddress)表服务实现类
 *
 * @author hui
 * @since 2022-07-13 17:26:33
 */
@Service("userAddressService")
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Override
    public ResponseResult getList(Long uid, int page, int limit) {

        Page<UserAddress> pageModel = new Page<>((long) page, (long) limit);
        Page<UserAddress> userAddressPage = this.lambdaQuery().eq(UserAddress::getUid, uid).page(pageModel);
        List<UserAddress> addresses = userAddressPage.getRecords();
        List<UserAddressVo> userAddressVos = BeanCopyUtils.copyBeanList(addresses, UserAddressVo.class);
        return ResponseResult.okResult(userAddressVos);
    }


    @Override
    public ResponseResult addAndEdit(Long uid, AddressParam addressParam) {
        UserAddress userAddress = UserAddress.builder()
                .city(addressParam.getCity()) //收货人所在市
                .district(addressParam.getDistrict()) //收货人所在区
                .province(addressParam.getProvince())//收货人所在省
                .detail(addressParam.getDetail()).uid(uid)//收货人详细地址
                .phone(addressParam.getPhone())//收货人电话
                .postCode(addressParam.getPostCode())//邮编
                .realName(addressParam.getRealName()) //收货人姓名
                .isDefault(addressParam.getIsDefault())//是否默认
                .build();
        if (addressParam.getIsDefault() == 1) {//设置默认
            UserAddress address = new UserAddress();
            address.setIsDefault(ShopCommonEnum.DEFAULT_0.getValue());//构造一个默认地址为0的对象
            ((UserAddressMapper)this.baseMapper).update(address,new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUid, uid));
        }
        //判断传来的参数，是否有地址id ,有说明数据库中已存在进行修改
        System.out.println();
        if (ObjectUtil.isNotNull(addressParam.getId())) {
            userAddress.setId(addressParam.getId());
            this.updateById(userAddress);
        } else {
            save(userAddress);
        }
        Map map = new HashMap();
        map.put("id", userAddress.getId());
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult getDetail(Long id) {
        UserAddress byId = getById(id);
        UserAddressVo userAddressVo = BeanCopyUtils.copyBean(byId, UserAddressVo.class);
        return ResponseResult.okResult(userAddressVo);
    }

    @Override
    public void setDefault(Long uid, Long addressId) {
        //TODO 设置默认地址，感觉可以优化
//        UserAddress address = new UserAddress();
//        address.setIsDefault(ShopCommonEnum.DEFAULT_0.getValue());
//        this.userAddressMapper.update(address, (Wrapper)(new LambdaQueryWrapper()).eq(UserAddress::getUid, uid));
//        UserAddress userAddress = new UserAddress();
//        userAddress.setIsDefault(ShopCommonEnum.DEFAULT_1.getValue());
//        userAddress.setId(addressId);
//        this.userAddressMapper.updateById(userAddress);
    }
}

